home *** CD-ROM | disk | FTP | other *** search
/ Aminet 49 / Aminet 49 (2002)(GTI - Schatztruhe)[!][Jun 2002].iso / Aminet / dev / src / 3d-fraktal.lha / 3D-Fraktal / 3D-Fraktal.C < prev    next >
C/C++ Source or Header  |  2002-02-21  |  3KB  |  166 lines

  1. /*  3D Fraktalstern
  2.  *  Autor: Norman Walter, Universität Stuttgart
  3.  *  Datum: 21.2.2002
  4.  *  3-dimensionales, animiertes, rekursives Fraktal mit OpenGL.
  5.  *  Leichte Abwandlung des Fraktalalgorithmus aus dem Buch "Algorithmen in C".
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <GL/glut.h>
  11.  
  12. /* Id's für das Menü */
  13.  
  14. #define ANIMATE 10
  15. #define QUIT 100
  16.  
  17. static GLboolean Animate = GL_TRUE;
  18.  
  19. double winkel = 0.0;
  20.  
  21. void malen(void);
  22. void malen_anstossen(void);
  23. void box(int x, int y, int r);
  24.  
  25. GLuint DasFraktal;
  26.  
  27. static void star( int x, int y, int r)
  28.  
  29.    /* Zeichnet Fraktalstern durch rekursive Funktionsaufrufe */
  30.  
  31. {
  32.     if (r>0)
  33.         {
  34.             star(x-r,y+r,r/2);
  35.             star(x+r,y+r,r/2);
  36.             star(x-r,y-r,r/2);
  37.             star(x+r,y-r,r/2);
  38.             box(x,y,r);
  39.         }
  40.  
  41. }
  42.  
  43. static void ModeMenu(int entry)
  44. {
  45.    /* Menü abfragen */
  46.  
  47.    if (entry==ANIMATE) {
  48.       Animate = !Animate;
  49.    }
  50.    else if (entry==QUIT) {
  51.       exit(0);
  52.    }
  53.  
  54.    glutPostRedisplay();
  55. }
  56.  
  57. int main (int argc, char **argv)
  58. {
  59.   /* Farbe und Position der Lichtquelle */
  60.  
  61.  GLfloat light0_pos[] = {25.,25.,80.,1.0};
  62.  GLfloat light0_color[] = {1.0,1.0,1.0,1.0};
  63.  GLfloat ambient_light[] = {0.5,0.5,0.5,1.0};
  64.  
  65.  glutInit(&argc, argv);
  66.  
  67.  /* Double Buffer und Depth Buffer aktivieren */
  68.  
  69.  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  70.  glutInitWindowSize(400, 300);
  71.  glutInitWindowPosition(100, 100);
  72.  glutCreateWindow("3D-Fraktal");
  73.  
  74.  glClearColor(0.0, 0.0, 0.0, 0.0);
  75.  
  76.    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient_light);
  77.    glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
  78.    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
  79.  
  80.    glFrontFace(GL_CW);
  81.    glEnable(GL_LIGHTING);
  82.    glEnable(GL_LIGHT0);
  83.  
  84.    glEnable(GL_DEPTH_TEST);
  85.    glDepthFunc(GL_LESS);
  86.  
  87.    glEnable(GL_AUTO_NORMAL);
  88.     glEnable(GL_NORMALIZE);
  89.  
  90.  /* Fraktal in eine Display-Liste schreiben */
  91.  
  92.    DasFraktal = glGenLists (1);
  93.    glNewList(DasFraktal, GL_COMPILE);
  94.    star(0,0,12);
  95.    glEndList();
  96.  
  97.  glMatrixMode(GL_PROJECTION);
  98.  
  99.  glLoadIdentity();
  100.  glFrustum(-4.,4.,-4.,4., 10.,80.);
  101.  
  102.  glTranslatef(0.,0.,-45.);
  103.  
  104.  glutDisplayFunc(&malen);
  105.  glutIdleFunc(&malen_anstossen);
  106.  
  107.  /* Menü hinzufügen */
  108.  
  109.  glutCreateMenu(ModeMenu);
  110.  glutAddMenuEntry("Toggle Animation", ANIMATE);
  111.  glutAddMenuEntry("Quit", QUIT);
  112.  glutAttachMenu(GLUT_RIGHT_BUTTON);
  113.  
  114.  glutMainLoop();
  115.  
  116.  return(0);
  117.  
  118. }
  119.  
  120. void malen(void)
  121. {
  122.  
  123.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  124.  
  125.   glMatrixMode(GL_MODELVIEW);
  126.   glLoadIdentity();
  127.  
  128.   glRotatef(winkel,1.0,1.0,1.0);
  129.  
  130.   /* Fraktal zeichnen */
  131.  
  132.   glCallList(DasFraktal);
  133.  
  134.   glFlush();
  135.   glutSwapBuffers();
  136. }
  137.  
  138. void malen_anstossen(void)
  139. {
  140.  
  141.   /* Wir drehen das Ganze in 3 Achsen */
  142.  
  143.   if (Animate) {
  144.     winkel = winkel + 1;
  145.     if (winkel > 360.0)
  146.        winkel = winkel - 360.0;
  147.     glutPostRedisplay();
  148.   }
  149. }
  150.  
  151. void box(int x,int y,int r)
  152.  
  153. {
  154.    /* Zeichnet einen Würfel mit Radius r an den Koordinaten x,y */
  155.  
  156.   glPushMatrix();
  157.  
  158.   glTranslatef(GLfloat(x),GLfloat(y),GLfloat(r));
  159.   glutSolidCube(GLfloat(r));
  160.  
  161.   glPopMatrix();
  162. }
  163.  
  164.  
  165.  
  166.